﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Encodings
{
    class Encodings
    {
        public static void ConvertFromToEncoding(string fromFilename, string toFileName)
        {
            Encoding sourceEncoding = Encoding.GetEncoding("windows-1251");
                Encoding outEncoding = Encoding.Unicode;
            StreamReader inputFile = new StreamReader(fromFilename ,sourceEncoding );
            using (inputFile)
            {
                StreamWriter outputFile = new StreamWriter(toFileName);
                using (outputFile)
                {
                    string line = inputFile.ReadLine();
                    while (line != null)
                    {
                        byte[] bytesInOldEncoding = sourceEncoding.GetBytes(line);
                        byte[] bytesInNewEncoding= Encoding.Convert(sourceEncoding, outEncoding, bytesInOldEncoding);
                        string lineToWrite = outEncoding.GetString(bytesInNewEncoding);
                        outputFile.WriteLine(lineToWrite);
                        line = inputFile.ReadLine();
                    }
                }
                
            }
        }

        static void Main(string[] args)
        {
            ConvertFromToEncoding("FileWith1251Encoding.txt", "utf8.txt");
        }
    }
}
